//- Copyright (C) 2023 Beijing Huaxia Chunsong Technology Co., Ltd. 
//- <https://www.chatopera.com>, Licensed under the Chunsong Public 
//- License, Version 1.0  (the "License"), https://docs.cskefu.com/licenses/v1.html
//- Unless required by applicable law or agreed to in writing, software
//- distributed under the License is distributed on an "AS IS" BASIS,
//- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//- See the License for the specific language governing permissions and
//- limitations under the License.
//- Copyright (C) 2018-Jun. 2023 Chatopera Inc, <https://www.chatopera.com>
//- Licensed under the Apache License, Version 2.0
//- http://www.apache.org/licenses/LICENSE-2.0
.ukefu-contacts-embed
    .row
        .col-lg-12
            h1.site-h1.ukefu-tab(style='border-top:1px solid #e6e6e6;height:26px;')
                span.ukefu-bt
                    i.layui-icon.ukewo-btn(style='font-size:20px;') 
                    .ukefu-bt-text
                        .ukefu-bt-text-title 全部联系人（#{contactsList.totalElements}）
                .ukefu-bt-text-content(style='position: absolute;right: 5px;top: 0px;')
                    form.layui-form(name='contactsSearch', action='/apps/contacts/embed/index.html', data-toggle='ajax-form', data-close='false', data-target='#mainajaxwin', style='float: left;')
                        .layui-form-item
                            .layui-inline
                                .layui-input-inline(style='width: 220px')
                                    input.layui-input(type='text', name='q', value=q, style='height: 30px;line-height: 30px;margin-top: 4px;border: 1px solid #32c24d;', placeholder='请输入联系人姓名或电话', autocomplete='off')
                                    i.layui-icon(style='position: absolute;cursor: pointer;right: 3px;top: 4px;font-size: 20px;color:#32c24d;') 
                                    //i.layui-icon(onclick="$('#contactsFormSubmit')".click(), style='position: absolute;cursor: pointer;right: 3px;top: 4px;font-size: 20px;color:#32c24d;') 
                                    //button(type="submit" id="contactsFormSubmit")
                    .layui-btn-group
                        a.layui-btn.layui-btn-small(href='/apps/contacts/embed/add.html?agentserviceid=#{agentserviceid}', title='新建联系人', data-toggle='load', data-target='#mainajaxwin')
                            i.layui-icon 
                            | 新建联系人
            table.layui-table(lay-skin='line', style='table-layout: fixed;word-break: break-all;')
                thead
                    tr.design-sortable-tr
                        th(width='20px')
                        th(width='30%') 联系人姓名
                        th(width='140px') 手机
                        th 邮件
                        th(width='50px') 性别
                        th 类型
                        th(width='70px') 操作
                tbody
                    if contactsList && contactsList.content && contactsList.content.size() > 0
                        for contacts in contactsList.content
                            tr.tdHasContorllor.design-dropable-td.ukefu-contacts
                                td(style="width:1px;padding:10px 0px 10px 10px;")
                                    input(type="checkbox",checked=(currentContacsId == contacts.id ? 'checked' : false),name="id" class="ctxid" value=contacts.id, id="contacts_#{contacts.id}")
                                    if currentContacsId == contacts.id && currentAgentUserContactsId
                                        input#currentContacsId(type="text", value=currentAgentUserContactsId, style="display: none")
                                td(title=contacts.name,style="text-overflow: ellipsis;white-space: nowrap;overflow: hidden;")
                                    | #{contacts.name}
                                td
                                    span.ukefu-phone-number
                                        | #{contacts.mobileno}
                                td
                                    | #{contacts.email}
                                td
                                    if contacts.gender && contacts.gender == '1'
                                        | 男
                                    if contacts.gender && contacts.gender == '0'
                                        | 女
                                    if contacts.gender && contacts.gender == '-1'
                                        | 未知
                                td
                                    | #{uKeFuDic[contacts.ckind].name}
                                td(style="white-space:nowrap;width:1%;" nowrap="nowrap")
                                    a(href="/apps/contacts/embed/edit.pug?id="+contacts.id+"&agentserviceid="+agentserviceid,data-toggle="load" data-target="#mainajaxwin")
                                        i.layui-icon &#xe642; 编辑
                    else
                        tr
                            td(colspan="8" style="height:400px;")
                                .ukefu-empty(style="background: none")
                                    i.layui-icon &#xe63a;
                                    p= '还没有联系人信息'
    .row(style='padding:5px;')
        .col-lg-12#page(style='text-align:center;')
.layui-form-button
    .layui-button-block
        button.layui-btn(lay-submit, lay-filter='contactsForm') 立即提交
        button.layui-btn.layui-btn-original(type='reset', onclick='layer.close(top.layerwin);') 关闭
script.
    $("input:checkbox").click(function () {
        if (!$(this).is(':checked')) {
            setTimeout(function () {
                $("input[type='checkbox']").prop("checked", false);
            }, 0)

        }
    });

    layui.use('form', function () {
        var form = layui.form();
        form.on('submit(contactsForm)', function (data) {
            var targetIFrame = eval(iframe);
            var selectedContactId = $("input:checked.ctxid").val();
            if (selectedContactId) {
                targetIFrame.Proxy.execLinkContactsFunction(selectedContactId);
            } else {
                var currentAgentUserContactsId = $("#currentContacsId").val();
                targetIFrame.Proxy.execCancelContactsFunction(currentAgentUserContactsId);
            }

            layer.closeAll("page");
            return false;
        });
    });

    layui.use('layer', function () {
        var layer = layui.layer;
        var msg = '#{msg}';
        if (msg && msg == 'new_contacts_success')
            layer.msg('联系人添加成功', {icon: 1, time: 1500})
        else if (msg && msg == 'new_contacts_fail')
            layer.msg('联系人添加失败，因为存在相同skypeid', {icon: 2, time: 1500})
        else if (msg && msg == 'edit_contacts_success')
            layer.msg('联系人编辑成功', {icon: 1, time: 1500})
        else if (msg && msg == 'edit_contacts_fail')
            layer.msg('联系人编辑失败，因为存在相同Skype ID', {icon: 2, time: 1500})
    });

    layui.use(['laypage'], function () {
        var laypage = layui.laypage;

        laypage({
            cont: 'page',
            pages: '#{contactsList ? contactsList.totalPages : 0}', //总页数
            curr: '#{contactsList ? contactsList.number + 1 : 0}',
            groups: 5 //连续显示分页数
            , jump: function (data, first) {
                if (!first) {
                    loadURL("/apps/contacts/embed/index.html?#{q?'q=q&':''}p=" + data.curr, "#mainajaxwin");
                }
            }
        });
    });
    $('.ukefu-contacts td:not(:last-child)').on("click", function () {
        $('.ctxid').prop("checked", false);
        if ($(this).parent().find("input").prop("checked")) {
            $(this).parent().find("input").prop("checked", false);
        } else {
            $(this).parent().find("input").prop("checked", true);
        }
    });
            
